#include "main.h"

/*Global variable*/
uint8_t Frame[SIZE_FRAME];
uint8_t HexFrame[SIZE_FRAME_HEX];
uint8_t Count_Frame = 0;
uint32_t Amount_Frame;
uint32_t Entry_Point_Image;
__IO uint8_t Boolen = 0;

Frame_TypeDef Frame_Data;

/* Main function */

int main (void)
{
	int i,j;
	uint8_t FlagMode;			/*Flag indicate enter to Bootloader mode or Application mode*/
													
	/* Update system clock */
	SystemCoreClockUpdate();
	/*Config SysTick interrupt ervery 1ms*/
	SysTick_Config(SystemCoreClock/1000);												
	
	FlagMode = SW1_Read();
	if(!FlagMode){
		printf("\r\nEnter to Bootloader mode");
		/*Init UART0*/
		UART0_PinConf();
		UART0_Init();                    
		UART0RX_EnableIRQ();
		
		/*Init PIT module*/
		PIT_InitTimer();
		PIT_ConfigValue(TIMER0, 100);
		PIT_EnableIRQ(TIMER0, MODE_NOCHAIN);
		printf("\r\nReady to receive data.....");
	}
	else{
		printf("\r\nEnter to Application mode");
	}
														
	while(1){
		if(!FlagMode){																				/*If SW1 press, enter Bootloader Mode*/
		if(Boolen==1){
			Boolen=0;																						/*receive enough data, clear flag to receive new frame*/
			Convert2Hex(Frame, HexFrame);												/*Convert frame received to frame hex*/
			
			/*process first frame received*/
			if(Count_Frame==1){																	/*the first frame encode amount of frame*/
				Amount_Frame = Cover_AmountHex_Frame(HexFrame);		/*Calculate amount of frame file srec received*/
				printf("\r\nAmount Frame = %d", Amount_Frame);	
			}		
			/*process frame data next*/
			else{	
				printf("\r\nStart checksum frame[%d] is received.........",Count_Frame-1);	
				if(Checksum_Data(HexFrame))				
				{
					/*Get data from frame received*/
					Cover_Data(HexFrame,Frame_Data.Data);
					/*Get address from frame received*/
					Frame_Data.Address = Cover_Address(HexFrame);	
					if(Count_Frame == Amount_Frame + 2){
						/*process the last frame*/
						printf("\r\nProcess the last frame");
						/*Get entry point image*/
						Entry_Point_Image = Frame_Data.Address;
						printf("\r\nEntry point image = 0x%x", Entry_Point_Image);						
						
						Count_Frame = 0;					
						printf("\r\nWrite complete !");
						/*Prepare jump to application*/
						/*Disable intterrupt*/
						NVIC_DisableIRQ(UART0_IRQn);
						NVIC_DisableIRQ(PIT_IRQn);
						printf("\rJump to Application adrress\n");
						/*jump to application*/
						StartApplication(SRAM_ADDR_BASE,Entry_Point_Image);		
					}
					else{
						printf("\r\nWrite Data Frame[%d] to RAM @ 0x%x......",Count_Frame-1,Frame_Data.Address);		
						/*Write data to RAM*/
						RAM_WriteData(( uint8_t*)Frame_Data.Address, Frame_Data.Data, SIZE_DATA);		
					}						
				}
				else{
					printf("Data Frame [%d] is error. Please send again\r\n",Count_Frame-1);
					Count_Frame = 0;	
				}
			}
		}
	}
		else{
			/*Jump to application*/
			JumpToApplication(SRAM_ADDR_BASE);
		}
	}
	return 0;
}